﻿using BusinessService.proto;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace BusinessService
{
    public class ResourceProvider
    {
        private static ResArray _resArray;

        static ResourceProvider()
        {
            OriginalBusiness business = new OriginalBusiness();
            _resArray = ResArray.ParseFrom(business.GetStaticResource());
        }

        private ResItem[] _productTypes;
        public ResItem[] ProductTypes
        {
            get
            {
                if (_productTypes == null)
                {
                    _productTypes = GetResItems(1).ToArray();
                }
                return _productTypes;
            }
        }

        private ResItem[] _useArea;
        public ResItem[] UseArea
        {
            get
            {
                if (_useArea == null)
                {
                    _useArea = GetResItems(2).ToArray();
                }
                return _useArea;
            }
        }


        private ResItem[] _orgin;
        public ResItem[] Orgin
        {
            get
            {
                if (_orgin == null)
                {
                    _orgin = GetResItems(3).ToArray();
                }
                return _orgin;
            }
        }

        private ResItem[] _defendType;
        public ResItem[] DefendType
        {
            get
            {
                if (_defendType == null)
                {
                    _defendType = GetResItems(4).ToArray();
                }
                return _defendType;
            }
        }

        private ResItem[] _lightingExplain;
        public ResItem[] LightingExplain
        {
            get
            {
                if (_lightingExplain == null)
                {
                    _lightingExplain = GetResItems(5).ToArray();
                }
                return _lightingExplain;
            }
        }

        private ResItem[] _workVoltage;
        public ResItem[] WorkVoltage
        {
            get
            {
                if (_workVoltage == null)
                {
                    _workVoltage = GetResItems(6).ToArray();
                }
                return _workVoltage;
            }
        }

        private ResItem[] _controlType;
        public ResItem[] ControlType
        {
            get
            {
                if (_controlType == null)
                {
                    _controlType = GetResItems(7).ToArray();
                }
                return _controlType;
            }
        }

        private ResItem[] _lightingType;
        public ResItem[] LightingType
        {
            get
            {
                if (_lightingType == null)
                {
                    _lightingType = GetResItems(8).ToArray();
                }
                return _lightingType;
            }
        }

        private ResItem[] _setupType;
        public ResItem[] SetupType
        {
            get
            {
                if (_setupType == null)
                {
                    _setupType = GetResItems(9).ToArray();
                }
                return _setupType;
            }
        }

        public IEnumerable<ResItem> GetResItems(int guid)
        {
            return from r in _resArray.ResList
                   where r.Id == guid
                   orderby r.ItemID
                   select r;
        }

        public ResItem GetResItem(int guid, string itemName)
        {
            return (from r in _resArray.ResList
                    where r.Id == guid && r.ItemName == itemName
                    select r).FirstOrDefault();
        }
    }
}
